package com.mxy.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mxy.myapplication.R;
import com.mxy.myapplication.adapter.AdapterForRecyclerView_Snap;
import com.mxy.myapplication.pojo.Pojo1;
import com.mxy.myapplication.util.LinearLayoutColorDivider;

import java.util.ArrayList;

/**
 * 阻尼效果的RecyclerView
 */

public class FragmentOfViewPager_Snap extends Fragment {

    private ArrayList<Pojo1> data = new ArrayList<>();
    private LinearLayoutManager llm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_linearlayout_itemtouchhelper, container, false);
        RecyclerView rv = (RecyclerView) v.findViewById(R.id.rv);

        llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(llm);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new LinearLayoutColorDivider(getActivity().getResources(),
                R.color.EFEFEF, R.dimen.dip1, LinearLayoutManager.VERTICAL));
        rv.addOnScrollListener(new OnScrolling());
        initData();
        AdapterForRecyclerView_Snap adapter = new AdapterForRecyclerView_Snap(getActivity(), data);
        rv.setAdapter(adapter);

        return v;
    }

    public void initData() {
        data.clear();
        data.add(new Pojo1(1, R.drawable.jj1, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj2, "进击的巨人"));
        data.add(new Pojo1(0, R.drawable.jj3, "进击的巨人"));
        data.add(new Pojo1(1, R.drawable.bleach1, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach2, "死神"));
        data.add(new Pojo1(0, R.drawable.bleach3, "死神"));
        data.add(new Pojo1(1, R.drawable.guanlan1, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan2, "灌篮高手"));
        data.add(new Pojo1(0, R.drawable.guanlan3, "灌篮高手"));
    }

    private class OnScrolling extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == 0) {//停止状态
                int position = llm.findFirstVisibleItemPosition();
                View v = llm.findViewByPosition(position);
                if (v.getTop() != 0 && llm.findLastCompletelyVisibleItemPosition() != data.size() - 1) {
                    if (Math.abs(v.getTop()) >= (v.getBottom() - v.getTop()) / 2) {//过了一半
                        View nextView = llm.findViewByPosition(position + 1);
                        recyclerView.smoothScrollBy(0, nextView.getTop());
                    } else {//没到一半
                        recyclerView.smoothScrollBy(0, v.getTop());
                    }
                }
            }
        }
    }
}